from flask.app import Flask
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField, BooleanField, ValidationError
from wtforms.validators import DataRequired, Length, Email, EqualTo, Regexp

from FileShare.models import User

# wtf注册用户表单
class RegisterForm(FlaskForm):
    # 暂时只提供用户名和密码以及邮箱即可以注册成功
    
    # Text Field类型，文本输入框，必填，用户名长度为4到25之间
    username = StringField('Username', validators=[DataRequired(), Length(1, 20), Regexp('^[a-zA-Z0-9]*$', message='The username should contain only a-z, A-Z and 0-9.')])
    
    # Text Field类型，密码输入框，必填，必须同password2字段一致
    password = PasswordField('Password', validators=[DataRequired(), Length(8, 128), EqualTo('password2')])
    password2 = PasswordField('Confirm password', validators=[DataRequired()])

    # 邮件地址
    email = StringField('Email', validators=[DataRequired(), Length(1, 254), Email()])
    
    # 提交按钮
    submit = SubmitField('Register')

    def validate_email(self, field):
        if User.query.filter_by(email=field.data.lower()).first():
            raise ValidationError('The email is already in use.')
    
    def validate_username(self, field):
        if User.query.filter_by(username=field.data).first():
            raise ValidationError('The username is already in use.')

# wtf登录用户表单
class LoginForm(FlaskForm):
    # Text Field类型，文本输入框，必填，用户名长度为4到25之间
    username = StringField('Username', validators=[Length(min=4, max=25)])

    # Text Field类型，密码输入框，必填
    password = PasswordField('Password', validators=[DataRequired()])

    # 记住登陆表单
    remember = BooleanField('Remember me')

    # submit按钮
    submit = SubmitField('Login')


